/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.aeso.betabit.dao;

import br.aeso.betabit.util.JPAHelper;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Edson
 */
public abstract class AbstractDao<T> implements IDao<T>{
    
    protected EntityManagerFactory factory;

	public AbstractDao() {
		factory = Persistence.createEntityManagerFactory("BetABitPU");
	}

	@Override
	public void inserir(final T entity) {
		JPAHelper helper = new JPAHelper(factory) {
			@Override
			protected void execute(EntityManager manager) {
				manager.persist(entity);
			}
		};
		helper.execute();
	}

    @Override
	public void atualizar(final T entity) {
		JPAHelper helper = new JPAHelper(factory) {
			@Override
			protected void execute(EntityManager manager) {
				manager.merge(entity);
			}
		};
		helper.execute();
	}
	
    @Override
	public void excluir(final T entity) {
		JPAHelper helper = new JPAHelper(factory) {
			@Override
			protected void execute(EntityManager manager) {
				manager.remove(entity);
			}
		};
		helper.execute();
	}
}